﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using holysheng.FileX;

namespace ShortCut
{
    public partial class MainForm : Form
    {
        holysheng.FileX.ShortcutControl shortcutControl;
        string currentDir = "";
        public MainForm()
        {
            InitializeComponent();
        }

        // 加载文件列表
        private void LoadFiles(string dir)
        {
            dataGridView_Old.Rows.Clear();
            shortcutControl = new ShortcutControl();
            BindingList<ShortcutAttributes> list = shortcutControl.GetFileList(dir);
            foreach (ShortcutAttributes file in list)
            {
                int index = this.dataGridView_Old.Rows.Add();
                this.dataGridView_Old.Rows[index].Cells[0].Value = file.FileOnlyPath + file.FileOnlyName + file.FileOnlyExtName;
                this.dataGridView_Old.Rows[index].Cells[1].Value = file.FileOnlyName;
                this.dataGridView_Old.Rows[index].Cells[2].Value = file.TargetPath;
                this.dataGridView_Old.Rows[index].Cells[3].Value = file.WorkingDirectory;
            }
            if (dataGridView_Old.Rows.Count > 0)
            {
                textBox_OldPath.Text = dataGridView_Old.Rows[0].Cells[3].Value.ToString();
                textBox_NewPath.Text = dataGridView_Old.Rows[0].Cells[3].Value.ToString();
            }
        }

        private void dataGridView_Old_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            dataGridView_New.Rows.Clear();
            textBox_OldPath.Text = dataGridView_Old.Rows[e.RowIndex].Cells[3].Value.ToString();
            textBox_NewPath.Text = dataGridView_Old.Rows[e.RowIndex].Cells[3].Value.ToString();
        }

        // 打开文件夹事件
        private void Button_OpenDir_Click(object sender, EventArgs e)
        {
            dataGridView_Old.Rows.Clear();
            FolderBrowserDialog folderBrowserDialog选择目录 = new FolderBrowserDialog();
            if (folderBrowserDialog选择目录.ShowDialog() == DialogResult.OK && folderBrowserDialog选择目录.SelectedPath != string.Empty)
            {
                currentDir = folderBrowserDialog选择目录.SelectedPath;
                LoadFiles(currentDir);
            }
        }

        // 预览事件
        private void Button_Show_Click(object sender, EventArgs e)
        {
            // todo 要改进，不区分大小写的判断即替换方法
            // 此处所有的字符串，都要同时转换为大写或小写
            if (textBox_OldPath.Text != "")
            {
                dataGridView_New.Rows.Clear();
                foreach (DataGridViewRow row in dataGridView_Old.Rows)
                {
                    // 只显示可以替换的列表：目标文件、起始位置
                    if (row.Cells[2].Value.ToString().ToLower().StartsWith(textBox_OldPath.Text.ToLower()) || row.Cells[3].Value.ToString().ToLower().StartsWith(textBox_OldPath.Text.ToLower()))
                    {
                        // 只显示将替换的列表：如果旧和新都一样
                        if (textBox_OldPath.Text.ToLower() != textBox_NewPath.Text.ToLower())
                        {
                            int indx = dataGridView_New.Rows.Add();
                            dataGridView_New.Rows[indx].Cells[0].Value = row.Cells[0].Value; // 文件全路径
                            dataGridView_New.Rows[indx].Cells[1].Value = row.Cells[1].Value; // 文件名
                            dataGridView_New.Rows[indx].Cells[2].Value = row.Cells[2].Value; // 原目标文件
                            dataGridView_New.Rows[indx].Cells[3].Value = row.Cells[2].Value.ToString().ToLower().Replace(textBox_OldPath.Text.ToLower(), textBox_NewPath.Text.ToLower()); // 替换目标文件
                            dataGridView_New.Rows[indx].Cells[4].Value = row.Cells[3].Value; // 原起始位置
                            dataGridView_New.Rows[indx].Cells[5].Value = row.Cells[3].Value.ToString().ToLower().Replace(textBox_OldPath.Text.ToLower(), textBox_NewPath.Text.ToLower()); // 替换起始位置
                        }
                    }
                }
            }
        }

        // 替换事件
        private void Button_Change_Click(object sender, EventArgs e)
        {
            bool isOk = true;
            string str = "部份替换失败：";
            ShortcutAttributes shortcut = new ShortcutAttributes();
            foreach (DataGridViewRow row in dataGridView_New.Rows)
            {
                string fileAllName = row.Cells[0].Value.ToString();
                string newTargetPath = row.Cells[3].Value.ToString();
                string newWorkingDirectory = row.Cells[5].Value.ToString();
                if (!shortcut.EditShortcuts(fileAllName, newTargetPath, newWorkingDirectory))
                {
                    isOk = false;
                    str += row.Cells[1].Value.ToString() + "；";
                }
            }
            if (isOk)
            {
                dataGridView_New.Rows.Clear();
                MessageBox.Show("替换成功!");
            }
            else
            {
                toolStripStatusLabel1.Text = str;
            }
            LoadFiles(currentDir);
        }

        // 关闭事件
        private void button_Close_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}
